package com.guangyuan.network.interval;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.guangyuan.network.constants.SqlType;
import com.guangyuan.network.dao.DBFactory;
import com.guangyuan.network.dao.interfaces.sql.ProductAttrRelSql;
import com.guangyuan.network.dataobject.ProductAttrRel;
import com.guangyuan.network.datasource.pool.util.ConnectionUtil;
import com.guangyuan.network.external.ProductAttrRelOper;

public class ProductAttrRelCache extends DefaultCache implements
		ProductAttrRelOper {

	private static ProductAttrRelCache productAttrRelCache;

	@Override
	public boolean deleteProductAttrRel(int id) {
		Object[] parameters = new Object[] { "" + id };

		Integer result = (Integer) execute(parameters,
				ProductAttrRelSql.delete, SqlType.OTHER);

		if (result > 0) {
			return true;
		}

		return false;
	}

	@Override
	public boolean insertProductAttrRel(ProductAttrRel productAttrRel) {
		String[] parameters = new String[4];

		parameters[0] = productAttrRel.getProductId().toString();
		parameters[1] = productAttrRel.getProductAttrId().toString();
		parameters[2] = productAttrRel.getOperator();
		parameters[3] = productAttrRel.getValue();
		Integer result = (Integer) execute(parameters,
				ProductAttrRelSql.insert, SqlType.INSERT);
		if (result > 0) {
			return true;
		}

		return false;
	}

	@Override
	public List<ProductAttrRel> listProductAttrRelByProductId(int productId) {
		String[] parameters = new String[] { "" + productId };
		Connection conn = ConnectionUtil.getConnection();

		ResultSet rs = (ResultSet) DBFactory.getDefaultDao().executeSql(conn,
				ProductAttrRelSql.selectByProductId, parameters, SqlType.OTHER);
		List<ProductAttrRel> list = new ArrayList<ProductAttrRel>();
		if (null != rs) {
			try {
				while (rs.next()) {
					ProductAttrRel productAttrRel = new ProductAttrRel();
					productAttrRel.setId(rs.getInt(1));
					productAttrRel.setProductId(rs.getInt(2));
					productAttrRel.setProductAttrId(rs.getInt(3));
					productAttrRel.setGmtCreate(rs.getDate(4));
					productAttrRel.setGmtModified(rs.getDate(5));
					productAttrRel.setOperator(rs.getString(6));
					productAttrRel.setValue(rs.getString(7));

					list.add(productAttrRel);
				}
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
		return list;
	}

	public static ProductAttrRelCache getInstance() {
		if (null == productAttrRelCache) {
			productAttrRelCache = new ProductAttrRelCache();
		}
		return productAttrRelCache;
	}

	@Override
	public boolean updateProductAttrRel(ProductAttrRel productAttrRel) {
		Object[] parameters = new Object[3];
		parameters[0] = productAttrRel.getValue();
		parameters[1] = productAttrRel.getProductId();
		parameters[2] = productAttrRel.getProductAttrId();

		Integer result = (Integer) execute(parameters,
				ProductAttrRelSql.update, SqlType.OTHER);
		if (result > 0) {
			return true;
		}

		return false;

	}

	@Override
	public boolean deleteProductAttrRelByProductId(int id) {
		Object[] parameters = new Object[] { "" + id };

		Integer result = (Integer) execute(parameters,
				ProductAttrRelSql.deleteAttrByProductId, SqlType.OTHER);

		if (result > 0) {
			return true;
		}

		return false;
	}
}
